<template>
<div>
  <div class="login-content" @keydown.enter="logoOn">
    <div class="login-box">
      <div class="login-left">
        <img src="@/assets/images/login_5.png" alt="">
      </div>
      <div class="login-right">
        <div class="login-form">
          <div class="login-header">
            <div class="login-header-img">
              <img src="@/assets/images/user_03.png" alt="">
            </div>
            <div class="login-header-line"></div>
          </div>
          <div class="login-input-group">
            <div class="input-box">
              <input type="text" v-model="account" placeholder="输入用户名">
            </div>
            <div class="input-box">
              <input type="password" v-model="password" placeholder="输入密码">
            </div>
          </div>
          <div class="login-btn">
            <input type="button" value="登              录" @click="logoOn" />
          </div>
          <div class="register-tips">尚未注册，请
            <router-link to="/register">免费注册</router-link>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
</template>
<script>
export default {
  data() {
    return {
      account: "",
      password: ""
    };
  },
  methods: {
    logoOn() {
      if (!this.account) {
        this.$Notice.warning({
          title: "警告",
          desc: "请输入登录账号！"
        });

        return;
      }

      if (!this.password) {
        this.$Notice.warning({
          title: "警告",
          desc: "请输入登录密码！"
        });

        return;
      }

      this.axios
        .post("/api/user/logon", {
          account: this.account,
          password: this.password
        })
        .then(rs => {
          if (rs.datas[0]) {
            this.$store.commit('logOn', rs.datas[0]);

            if(rs.datas[0].UserType == '0'){
              this.$router.push('/behind/index');

              return;
            }

            if (this.$route.query.redirect) {
              this.$router.push(this.$route.query.redirect);
            } else {
              this.$router.push("/");
            }
          } else {
            this.$Notice.error({
              title: "失败",
              desc: "用户登录失败，用户名或密码错误！"
            });
          }
        });
    }
  },
  created() {
    let query = this.$route.query;

    if (query.tenantGuid && query.account && query.time && query.rnd && query.md5) {
      this.axios
        .post("/api/user/slogon", query)
        .then(rs => {
          if (rs.datas[0]) {
            window.sessionStorage.user = JSON.stringify({
              name: rs.datas[0].Name,
              corpName: rs.datas[0].CorpName,
              userType: rs.datas[0].UserType
            });

            if (this.$route.query.redirect) {
              this.$router.push(this.$route.query.redirect);
            } else {
              this.$router.push("/");
            }
          } else {
            this.$Notice.error({
              title: "失败",
              desc: "用户登录失败，用户名或密码错误！"
            });
          }
        });
    }
  }
};
</script>
